
using System.Collections.Generic;
using System.Linq;
using NiMVC;
using UnityEngine;


public class NumberHelp艺术数字: MonoBehaviour
{
    [SerializeField]private string sValue;
    [SerializeField]private Enum艺术字key枚举 type;
    [SerializeField]private Transform tran;
    [SerializeField]private Comp单个字体 Comp单个字体预制体;
    [SerializeField]private float size;
    [SerializeField]private List<Comp单个字体> list字体 = new List<Comp单个字体>();
    private List<Comp单个字体> listPool = new List<Comp单个字体>();

    public string text
    {
        get { return sValue; }
        set
        {
            sValue = value;
            SetValue(sValue);
        }
    }

    private void Clear()
    {
        foreach (var VARIABLE in list字体)
        {
            if(VARIABLE==null) continue;
            
            
            listPool.Add(VARIABLE);
            VARIABLE.gameObject.SetActive(false);
        }
        list字体.Clear();
    }

    private Comp单个字体 GetComp()
    {
        Comp单个字体 res = null;
        if (listPool.Count > 0)
        {
            res = listPool[0];
            listPool.RemoveAt(0);
        }
        else
        {
            res = GameObject.Instantiate(Comp单个字体预制体, tran);
        }

        res.transform.SetAsLastSibling();
        res.gameObject.SetActive(true);
        list字体.Add(res);
        return res;
        
    }

#if UNITY_EDITOR
    [ContextMenu("测试按钮")]
    public void TestSetValue()
    {
        var configs = UnityEditor.AssetDatabase.LoadAssetAtPath<艺术字Model>("Assets/Resource/生成的配置/Unity艺术字体配置.asset");
        
        艺术字体 ziti;
        var all字体 = configs.GetData();
        int index = all字体.FindIndex(f => f.type == type);
        if (index < 0)
        {
            Debug.LogError($"找不到字体配置{type}");
            index = all字体.FindIndex(f => f.type == Enum艺术字key枚举.主界面Test);
        }

        ziti = all字体[index];
        List<艺术字体.字体> res = new List<艺术字体.字体>();
        Dictionary<int, char> dictKeys = new Dictionary<int, char>();
        int i = 0;
        foreach (var VARIABLE in sValue)
        {
            var v = ziti.all字.FirstOrDefault(f => f.key == VARIABLE.ToString());
            if(v==null) continue;
            res.Add(v);
        }

        SetValue(res);
    }
#endif
    

    public void SetValue(List<艺术字体.字体> Sprites)
    {
        List<int> needRemove = new List<int>();
        for (int i = 0; i < Sprites.Count; i++)
        {
            if (list字体.Count > i && list字体[i].key == Sprites[i].key)
            {
                list字体[i].SetSize(size);
                continue;
            }
            else if (list字体.Count > i && list字体[i].key != Sprites[i].key)
            {
                list字体[i].SetUp(Sprites[i],size);
            }else if (list字体.Count <= i)
            {
                var comp = GetComp();
                comp.SetUp(Sprites[i],size);
            }
        }

        if (list字体.Count > Sprites.Count)
        {
            for (int i = list字体.Count-1; i >= Sprites.Count; i--)
            {
                listPool.Add(list字体[i]);
                list字体[i].gameObject.SetActive(false);
                list字体.RemoveAt(i);
            }
        }
    }

    public void SetValue(string s)
    {
        sValue = s;
        List<艺术字体.字体> values = new List<艺术字体.字体>();
        var model =ModelManager.Instance.GetModel<艺术字Model>();
        values = model.GetValues(sValue, type);
        SetValue(values);
        
    }
}

